<--- %%NOBANNER%% --> sscan.sas
 BackForward
/*-------------------<---Start of Description-->---------------------\
| Scan a string of word using a word or a substring as delimiter;    |
|---------------------<---End of Description-->----------------------|
|--------------------------------------------------------------------|
|------------<---Start of Files or Arguments Needed-->---------------|
| Inputs:                                                            |
|   _strx_ - the input string;                                       |
|   _cntx_ - the ith word;                                           |
|   _sepx_ - the delimiter;                                          |
|            note: it can take a string as a delimiter;              |
|------------------<---End of Arguments Needed-->--------------------|
|--------------------------------------------------------------------|
|------------------<---Start of Files Created-->---------------------|
| Example: %let x=this,test is a test;                               |
|          %put %sscan(%nrbquote(&x), 2, %str(is| |,));              |
| Usage: indexw(var,excerpt);                                        |
\-------------------<---End of Files Created-->---------------------*/
%macro sscan(_strx_, _cntx_, _sepx_);
%let _specialchar_=ØÙÚÛÜÝÞßøùúûüýþÿñðæÖ;
%if (%words(%nrbquote(&_sepx_), dlm=%nrbquote(|)) le 1) %then %do;
   %if (%length(&_sepx_) le 1) %then 
      %scan(%nrbquote(&_strx_), &_cntx_, %nrbquote(&_sepx_));
   %else %do;
      %let _strx_=%sysfunc(tranwrd(%nrbquote(&_strx_), %nrbquote(&_sepx_),%nrbquote(Ø)));
      %scan(%nrbquote(&_strx_), &_cntx_, %nrbquote(Ø));
   %end;
%end;
%else %do; 
	%let _wcounti_=0; %let _dlmx_=; %let _ispecialchar_=0;
	%do %while(%length(%nrbquote(%scan(%nrbquote(&_sepx_), %eval(&_wcounti_+1), %nrbquote(|)))));
	   %let _wcounti_=%eval(&_wcounti_+1);
	   %let _sepxi_=%nrbquote(%qscan(%nrbquote(&_sepx_), &_wcounti_, %nrbquote(|)));
      %if (%length(&_sepxi_) ge 2) %then %do;
         %let _ispecialchar_=%eval(&_ispecialchar_+1); %let _specialchari_=%substr(&_specialchar_, &_ispecialchar_, 1);
         %let _dlmx_=&_dlmx_.&_specialchari_;
         %let _strx_=%sysfunc(tranwrd(%nrbquote(&_strx_), %nrbquote(&_sepxi_),%nrbquote(&_specialchari_)));
      %end;
      %else %let _dlmx_=&_dlmx_.&_sepxi_;
	%end;
   %scan(%nrbquote(&_strx_), &_cntx_, %quote(&_dlmx_));
%end;
%mend sscan;